package com.xiaoxi.mediatormode.frame;

import com.xiaoxi.mediatormode.colleague.ColleagueButton;
import com.xiaoxi.mediatormode.colleague.ColleagueCheckBox;
import com.xiaoxi.mediatormode.colleague.ColleagueTextField;
import com.xiaoxi.mediatormode.mediator.Mediator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.mediatormode.frame
 * @ClassName LoginFrame
 * @Description TODO
 * @Date 2023/3/27 14:44
 * @Version 1.0
 */
public class LoginFrame extends Frame implements ActionListener, Mediator {

    private ColleagueCheckBox checkBoxGuest;
    private ColleagueCheckBox checkBoxLogin;

    private ColleagueTextField textFieldUsername;
    private ColleagueTextField textFieldPassword;

    private ColleagueButton buttonOk;
    private ColleagueButton buttonCancel;

    public LoginFrame(String title) throws HeadlessException {
        super(title);
        // 设置背景
        setBackground(Color.lightGray);

        // 设置布局 4 * 2
        setLayout(new GridLayout(4, 2));

        // 将组员初始化
        createColleague();

        add(checkBoxGuest);
        add(checkBoxLogin);
        add(textFieldUsername);
        add(textFieldPassword);
        add(buttonOk);
        add(buttonCancel);

        colleagueChanged();

        pack();
        show();
    }

    @Override
    public void createColleague() {
        CheckboxGroup checkboxGroup = new CheckboxGroup();
        checkBoxGuest = new ColleagueCheckBox("Guest", checkboxGroup, true);
        checkBoxLogin = new ColleagueCheckBox("Login", checkboxGroup, false);

        textFieldUsername = new ColleagueTextField("username");
        textFieldPassword = new ColleagueTextField("password");

        buttonOk = new ColleagueButton("OK");
        buttonCancel = new ColleagueButton("Cancel");

        // 将所有数据交给仲裁者。统一纳管
        checkBoxGuest.setMediator(this);
        checkBoxLogin.setMediator(this);
        textFieldUsername.setMediator(this);
        textFieldPassword.setMediator(this);
        buttonOk.setMediator(this);
        buttonCancel.setMediator(this);

        // 设置监听者
        checkBoxGuest.addItemListener(checkBoxGuest);
        checkBoxLogin.addItemListener(checkBoxLogin);
        textFieldUsername.addTextListener(textFieldUsername);
        textFieldPassword.addTextListener(textFieldPassword);
        buttonOk.addActionListener(this);
        buttonCancel.addActionListener(this);

    }

    @Override
    public void colleagueChanged() {
        if (checkBoxGuest.getState()) {
            textFieldUsername.setColleagueEnabled(false);
            textFieldPassword.setColleagueEnabled(false);
            buttonOk.setColleagueEnabled(true);
        } else {
            textFieldUsername.setColleagueEnabled(true);
            textFieldPassword.setColleagueEnabled(textFieldUsername.getText().length() > 0);
            buttonOk.setColleagueEnabled(textFieldPassword.getText().length() > 0);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
        System.exit(0);
    }


}
